package org.jeecg.modules.online.config.p022d;

import com.alibaba.druid.filter.config.ConfigTools;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.toolkit.JdbcUtils;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import javax.sql.DataSource;
import org.jeecg.common.util.CommonUtils;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.common.util.dynamic.db.DbTypeUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.online.auth.p004b.AuthConstants;
import org.jeecg.modules.online.config.exception.DBException;
import org.jeecg.modules.online.config.p020b.DataBaseConfig;
import org.jeecg.modules.online.config.p020b.Druid;
import org.jeecg.modules.online.config.service.DbTableHandleI;
import org.jeecg.modules.online.config.service.p023a.DbTableDB2HandleImpl;
import org.jeecg.modules.online.config.service.p023a.DbTableDmHandleImpl;
import org.jeecg.modules.online.config.service.p023a.DbTableHyperSQLHandleImpl;
import org.jeecg.modules.online.config.service.p023a.DbTableMysqlHandleImpl;
import org.jeecg.modules.online.config.service.p023a.DbTableOracleHandleImpl;
import org.jeecg.modules.online.config.service.p023a.DbTablePostgresHandleImpl;
import org.jeecg.modules.online.config.service.p023a.DbTableSQLServerHandleImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

/* renamed from: org.jeecg.modules.online.config.d.d */
/* loaded from: hibernate-re-3.7.1-RC.jar:org/jeecg/modules/online/config/d/d.class */
public class DbTableUtil {

    /* renamed from: b */
    private static final Logger f587b = LoggerFactory.getLogger(DbTableUtil.class);

    /* renamed from: a */
    public static String f588a = "";

    public static DbTableHandleI getTableHandle() throws SQLException, DBException {
        return m32a((DataBaseConfig) null);
    }

    /* renamed from: a */
    public static DbTableHandleI m32a(DataBaseConfig dataBaseConfig) throws SQLException, DBException {
        DbTableHandleI dbTableMysqlHandleImpl = null;
        DbType m27c = m27c(dataBaseConfig);
        String dbTypeString = DbTypeUtils.getDbTypeString(m27c);
        if (DbType.DM.equals(m27c)) {
            return new DbTableDmHandleImpl();
        }
        boolean z = true;
        switch (dbTypeString) {
            case "ORACLE":
            	 dbTableMysqlHandleImpl = new DbTableOracleHandleImpl();
                break;
            case "POSTGRESQL":
            	dbTableMysqlHandleImpl = new DbTablePostgresHandleImpl();
                break;
            case "DM":
            	dbTableMysqlHandleImpl = new DbTableDmHandleImpl();
                break;
            case "DB2":
            	dbTableMysqlHandleImpl = new DbTableDB2HandleImpl();
                break;
            case "HSQL":
            	dbTableMysqlHandleImpl = new DbTableHyperSQLHandleImpl();
                break;
            case "MYSQL":
            	 dbTableMysqlHandleImpl = new DbTableMysqlHandleImpl();
                break;
            case "SQLSERVER":
            	dbTableMysqlHandleImpl = new DbTableSQLServerHandleImpl();
                break;
            case "MARIADB":
            	dbTableMysqlHandleImpl = new DbTableMysqlHandleImpl();
                break;
        }
        return dbTableMysqlHandleImpl;
    }

    public static Connection getConnection() throws SQLException {
        return ((DataSource) SpringContextUtils.getApplicationContext().getBean(DataSource.class)).getConnection();
    }

    public static String getDatabaseType() throws SQLException, DBException {
        if (oConvertUtils.isNotEmpty(f588a)) {
            return f588a;
        }
        return m33a((DataSource) SpringContextUtils.getApplicationContext().getBean(DataSource.class));
    }

    /* renamed from: a */
    public static boolean m39a() {
        try {
            return "ORACLE".equals(getDatabaseType());
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } catch (DBException e2) {
            e2.printStackTrace();
            return false;
        }
    }

    /* renamed from: a */
    public static String m33a(DataSource dataSource) throws SQLException, DBException {
        if ("".equals(f588a)) {
            Connection connection = dataSource.getConnection();
            try {
                try {
                    String lowerCase = connection.getMetaData().getDatabaseProductName().toLowerCase();
                    if (lowerCase.indexOf("mysql") >= 0) {
                        f588a = "MYSQL";
                    } else if (lowerCase.indexOf("oracle") >= 0) {
                        f588a = "ORACLE";
                    } else if (lowerCase.indexOf("dm") >= 0) {
                        f588a = "DM";
                    } else if (lowerCase.indexOf("sqlserver") >= 0 || lowerCase.indexOf("sql server") >= 0) {
                        f588a = "SQLSERVER";
                    } else if (lowerCase.indexOf("postgresql") >= 0 || lowerCase.indexOf("kingbasees") >= 0) {
                        f588a = "POSTGRESQL";
                    } else if (lowerCase.indexOf("mariadb") >= 0) {
                        f588a = "MARIADB";
                    } else {
                        f587b.error("数据库类型:[" + lowerCase + "]不识别!");
                    }
                    if (connection != null && !connection.isClosed()) {
                        connection.close();
                    }
                } catch (Exception e) {
                    f587b.error(e.getMessage(), e);
                    if (connection != null && !connection.isClosed()) {
                        connection.close();
                    }
                }
            } catch (Throwable th) {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
                throw th;
            }
        }
        return f588a;
    }

    /* renamed from: a */
    public static String m35a(Connection connection) throws SQLException, DBException {
        if ("".equals(f588a)) {
            String lowerCase = connection.getMetaData().getDatabaseProductName().toLowerCase();
            if (lowerCase.indexOf("mysql") >= 0) {
                f588a = "MYSQL";
            } else if (lowerCase.indexOf("oracle") >= 0) {
                f588a = "ORACLE";
            } else if (lowerCase.indexOf("sqlserver") >= 0 || lowerCase.indexOf("sql server") >= 0) {
                f588a = "SQLSERVER";
            } else if (lowerCase.indexOf("postgresql") >= 0) {
                f588a = "POSTGRESQL";
            } else if (lowerCase.indexOf("mariadb") >= 0) {
                f588a = "MARIADB";
            } else {
                f587b.error("数据库类型:[" + lowerCase + "]不识别!");
            }
        }
        return f588a;
    }

    /* renamed from: a */
    public static String m37a(String str, String str2) {
        boolean z = true;
        switch (str2) {
            case "ORACLE":
            	return str.toUpperCase();
            case "POSTGRESQL":
            	return str.toLowerCase();
            case "DB2":
            	 return str;
        }
		return str;
    }

    /* renamed from: a */
    public static Boolean m38a(String str) {
        return m36a(str, (DataBaseConfig) null);
    }

    /* renamed from: a */
    public static Boolean m36a(String str, DataBaseConfig dataBaseConfig) {
        Connection m29b;
        String username;
        ResultSet tables;
        Connection connection = null;
        ResultSet resultSet = null;
        try {
            try {
                String[] strArr = {"TABLE"};
                if (dataBaseConfig == null) {
                    m29b = getConnection();
                } else {
                    m29b = m29b(dataBaseConfig);
                }
                DatabaseMetaData metaData = m29b.getMetaData();
                DbType m27c = m27c(dataBaseConfig);
                String m37a = m37a(str, DbTypeUtils.getDbTypeString(m27c));
                if (dataBaseConfig != null) {
                    username = dataBaseConfig.getUsername();
                } else {
                    username = ((DataBaseConfig) SpringContextUtils.getBean(DataBaseConfig.class)).getUsername();
                }
                if (DbTypeUtils.dbTypeIsOracle(m27c) || DbType.DB2.equals(m27c)) {
                    username = username != null ? username.toUpperCase() : null;
                }
                if (DbTypeUtils.dbTypeIsSqlServer(m27c)) {
                    tables = metaData.getTables(m29b.getCatalog(), null, m37a, strArr);
                } else if (DbTypeUtils.dbTypeIsPostgre(m27c)) {
                    tables = metaData.getTables(m29b.getCatalog(), m30b(m29b), m37a, strArr);
                } else if (DbType.HSQL.equals(m27c)) {
                    tables = metaData.getTables(m29b.getCatalog(), "PUBLIC", m37a.toUpperCase(), strArr);
                } else {
                    tables = metaData.getTables(m29b.getCatalog(), username, m37a, strArr);
                }
                if (tables.next()) {
                    if (tables != null) {
                        try {
                            tables.close();
                        } catch (SQLException e) {
                            f587b.error(e.getMessage(), e);
                        }
                    }
                    if (m29b != null) {
                        m29b.close();
                    }
                    return true;
                }
                if (tables != null) {
                    try {
                        tables.close();
                    } catch (SQLException e2) {
                        f587b.error(e2.getMessage(), e2);
                    }
                }
                if (m29b != null) {
                    m29b.close();
                }
                return false;
            } catch (SQLException e3) {
                throw new RuntimeException(e3.getMessage(), e3);
            }
        } catch (Throwable th) {
            if (0 != 0) {
                try {
                    resultSet.close();
                } catch (SQLException e4) {
                    f587b.error(e4.getMessage(), e4);
                    throw th;
                }
            }
            throw th;
        }
    }

    /* renamed from: b */
    public static String m30b(Connection connection) {
        try {
            ResultSet executeQuery = connection.createStatement().executeQuery("SELECT current_schema()");
            if (executeQuery.next()) {
                return executeQuery.getString(1);
            }
            return "public";
        } catch (SQLException e) {
            return "public";
        }
    }

    /* renamed from: a */
    public static Map<String, Object> m34a(List<Map<String, Object>> list) {
        HashMap hashMap = new HashMap(5);
        for (int i = 0; i < list.size(); i++) {
            hashMap.put(list.get(i).get("column_name").toString(), list.get(i));
        }
        return hashMap;
    }

    public static String getDialect() throws SQLException, DBException {
        return m31b(getDatabaseType());
    }

    /* renamed from: b */
    public static String m31b(String str) throws SQLException, DBException {
        String str2 = "org.hibernate.dialect.MySQL5InnoDBDialect";
        boolean z = true;
        switch (str) {
            case "ORACLE":
            	str2 = "org.hibernate.dialect.OracleDialect";
                break;
            case "POSTGRESQL":
            	str2 = "org.hibernate.dialect.PostgreSQLDialect";
                break;
            case "KINGBASEES":
                break;
            case "DM":
            	str2 = "org.hibernate.dialect.DmDialect";
                break;
            case "SQLSERVER":
            	str2 = "org.hibernate.dialect.SQLServerDialect";
                break;
        }
        return str2;
    }

    /* renamed from: c */
    public static String m28c(String str) {
        return str;
    }

    /* renamed from: b */
    public static Connection m29b(DataBaseConfig dataBaseConfig) throws SQLException {
        Druid druid;
        DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
        driverManagerDataSource.setDriverClassName(dataBaseConfig.getDriverClassName());
        driverManagerDataSource.setUrl(dataBaseConfig.getUrl());
        driverManagerDataSource.setUsername(dataBaseConfig.getUsername());
        String password = dataBaseConfig.getPassword();
        if (password != null && (druid = dataBaseConfig.getDruid()) != null && oConvertUtils.isNotEmpty(druid.getPublicKey())) {
            f587b.info("dbconfig.getDruid().getPublicKey() = {}", druid.getPublicKey());
            try {
                String decrypt = ConfigTools.decrypt(druid.getPublicKey(), password);
                f587b.debug("解密密码 decryptPassword = {}", decrypt);
                password = decrypt;
            } catch (Exception e) {
                f587b.error(e.getMessage(), e);
            }
        }
        driverManagerDataSource.setPassword(password);
        return driverManagerDataSource.getConnection();
    }

    /* renamed from: c */
    public static DbType m27c(DataBaseConfig dataBaseConfig) {
        if (dataBaseConfig == null) {
            return CommonUtils.getDatabaseTypeEnum();
        }
        return JdbcUtils.getDbType(dataBaseConfig.getUrl());
    }

    /* renamed from: d */
    public static ResourceBundle m26d(String str) {
        PropertyResourceBundle propertyResourceBundle = null;
        BufferedInputStream bufferedInputStream = null;
        try {
            bufferedInputStream = new BufferedInputStream(new FileInputStream(System.getProperty("user.dir") + File.separator + "config" + File.separator + str + ".properties"));
            propertyResourceBundle = new PropertyResourceBundle(bufferedInputStream);
            bufferedInputStream.close();
            if (propertyResourceBundle != null) {
            }
            if (bufferedInputStream != null) {
                try {
                    bufferedInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (IOException e2) {
            if (bufferedInputStream != null) {
                try {
                    bufferedInputStream.close();
                } catch (IOException e3) {
                    e3.printStackTrace();
                }
            }
        } catch (Throwable th) {
            if (bufferedInputStream != null) {
                try {
                    bufferedInputStream.close();
                } catch (IOException e4) {
                    e4.printStackTrace();
                }
            }
            throw th;
        }
        return propertyResourceBundle;
    }
}
